<?php
class Admini_ManageproblemController extends Uuch_ManageController{    
    
    public function init() {
        parent::init();
        $this->appendCss('/css/home/warehouse.css');
        $this->appendCss('/admins/css/common.css');
        $this->appendCss('/admins/css/yundan.css');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js'); 
        $this->view->countbillout1 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_StayUser);
        $this->view->countbillout2 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_StayUuch);
        $this->view->countbillout12 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_ReturnCancel);
        $this->view->countbillout11 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_PartReturn);
        $this->view->countbillout121 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_ReturnCancel,Application_Model_ProblemWay::Schedule_ApplyFor);
        $this->view->countbillout122 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_ReturnCancel,Application_Model_ProblemWay::Schedule_ReturnRoute);
        $this->view->countbillout123 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_ReturnCancel,Application_Model_ProblemWay::Schedule_InChannel);
        $this->view->countbillout124 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_ReturnCancel,Application_Model_ProblemWay::Schedule_InUuch);
        $this->view->countbillout111 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_PartReturn,Application_Model_ProblemWay::Schedule_ApplyFor);
        $this->view->countbillout112 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_PartReturn,Application_Model_ProblemWay::Schedule_ReturnRoute);
        $this->view->countbillout114 = Application_Model_ProblemWay::getProblemScheduleByCount(Application_Model_ProblemWay::Problem_PartReturn,Application_Model_ProblemWay::Schedule_InUuch);
        $this->view->type = 'billProblem';
    }
    

    public function  indexAction()
    {
        
    }
    
    public function problemAction()
    {
        $pagesize = $this->_getParam('pagesize');
        if(!$pagesize){ $pagesize=10; }
        $pageindex = $this->_getParam('start');
        if(!$pageindex){ $pageindex=0; }
        $balusc = $this->_getParam('balusc');
        $timescope = $this->_getParam('timescope');
        $starttime = $this->_getParam('starttime');
        $endtime = $this->_getParam('endtime');
        $invoice = $this->_getParam("invoice");
        $isinvoice = $this->_getParam("isinvoice");
        $channelid = $this->_getParam("channelid");
        $cityid = $this->_getParam("cityid");
        $startweight = $this->_getParam("startweight");
        $endweight = $this->_getParam("endweight");
        $startpayment = $this->_getParam("startpayment");
        $endpayment = $this->_getParam("endpayment");
        $startmoney = $this->_getParam("startmoney");
        $endmoney = $this->_getParam("endmoney");
        $agentid = $this->_getParam('agentid');
        $statusid = $this->_getParam('statusid');
        $stus = $this->_getParam('stus');
        $problem = $this->_getParam("problem");
        $isAddform = $this->_getParam("isAddform");
        $schedule = $this->_getParam("schedule");
        $isMessage = $this->getSafeParam("isMessage");
        $stype = 99;
        $status = "";
        if($stus != '' && $schedule == ""){
            $stype = $stus;
        } else {
            $stype = $stus.$schedule;
        }
        if($isMessage != ""){
            $stype = $isMessage;
        }
        if($statusid === "" || $statusid === null){
        }else{
            $status = $statusid;
            $stype = $status;
        }
        $strart = date("m")%3==0?date("m")/3:((int)(date("m")/3)+1);
        if($timescope != null){
            switch ($timescope) {
                case 'lastweek':
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-date("w")+1-7,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-date("w")+7-7,date("Y")));
                    break;
                case "week":
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-date("w")+7,date("Y")));
                    break;
                case "month":
                    $starttime =  date("Y-m-d",mktime(0,0,0,date("m"),1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("t"),date("Y")));
                    break;
                case "lastmonth":
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m")-1,1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m")-1,cal_days_in_month(CAL_GREGORIAN, date("m")-1, date("Y")),date("Y")));
                    break;
                case "quarter":
                    $starttime = date("Y-m-d",mktime(0,0,0,($strart-1)*3+1,1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,$strart*3,cal_days_in_month(CAL_GREGORIAN, $strart*3, date("Y")),date("Y")));
                    break;
                case "lastquarter":
                    $starttime = date("Y-m-d",mktime(0,0,0,($strart-1)*3-2,1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,($strart-1)*3,cal_days_in_month(CAL_GREGORIAN, ($strart-1)*3, date("Y")),date("Y")));
                    break;
                case "year":
                    $starttime = date("Y-m-d",mktime(0,0,0,1,1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,12,cal_days_in_month(CAL_GREGORIAN, 12, date("Y")),date("Y")));
                    break;
                case "lastyear":
                    $starttime = date("Y-m-d",mktime(0,0,0,1,1,date("Y")-1));
                    $endtime = date("Y-m-d",mktime(0,0,0,12,cal_days_in_month(CAL_GREGORIAN, 12, date("Y")-1),date("Y")-1));
                    break;
                case "day7":
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-6,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
                    break;
                case "day30":
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-30,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
                    break;
                default :
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-90,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
                    break;
            }
        }
        $problemwaySearch = new Application_Model_Filter_ProblemWaySearch();
        $problemwaySearch->balusc = $balusc;
        $problemwaySearch->agentid = $agentid;
        $problemwaySearch->channelid = $channelid;
        $problemwaySearch->starttime = $starttime;
        $problemwaySearch->endtime = $endtime;
        $problemwaySearch->statusid = $statusid;
        $problemwaySearch->invoice = $invoice;
        $problemwaySearch->cityid = $cityid;
        $problemwaySearch->startweight = $startweight;
        $problemwaySearch->endweight = $endweight;
        $problemwaySearch->startpayment = $startpayment;
        $problemwaySearch->endpayment = $endpayment;
        $problemwaySearch->startmoney = $startmoney;
        $problemwaySearch->endmoney = $endmoney;
        $problemwaySearch->isAddform = $isAddform;
        $problemwaySearch->isinvoice = $isinvoice;
        
        $billall = Application_Model_ProblemWay::getBillAll($problemwaySearch,$pagesize,$pageindex,$stus,$schedule,$isMessage);
//        var_dump($billall);exit;
        $this->view->arrsta = Application_Model_wayBillItem::getStatusEnum();
        $this->view->agentcomp = Application_Model_AgentCompany::getAgentAll();
        $this->view->expresscomp = Application_Model_Express::getAll();
        $words = array();
        for($i=0;$i<26;$i++){
            $words[]= chr(65+$i);
        }
        $this->view->words = $words;
        $this->view->arrstatus = Application_Model_wayBillItem::getInteriorAll();
        $billalllist = $billall['list'];
        $this->view->balusc = $balusc;
        $this->view->agentId = $agentid;
        $this->view->channelid = $channelid;
        $this->view->timescope = $timescope;
        $this->view->starttime = $starttime;
        $this->view->endtime = $endtime;
        $this->view->cityid = $cityid;
        $this->view->startweight = $startweight;
        $this->view->endweight = $endweight;
        $this->view->startpayment = $startpayment;
        $this->view->endpayment = $endpayment;
        $this->view->startmoney = $startmoney;
        $this->view->endmoney = $endmoney;
        $this->view->billall = $billall;
        $this->view->billlist = $billalllist;
        $this->view->pagesize = $pagesize;
        $this->view->invoice = $invoice;
        $this->view->status = $statusid;
        $this->view->isAddform = $isAddform;
        $this->view->stype = $stype;
    }
    
    public function scheduledefaultAction()
    {
        $billid = $this->_getParam("id");
        $ScheduleType = $this->_getParam("ScheduleType");
        $billItem = Application_Model_wayBillItem::load($billid);
        $result = $billItem->ScheduleDefault($ScheduleType);
        if($result){
            $this->_redirect('/admini/managewaybill/billaccep/ma/3/id/'.$billid);
        }else{
            $this->_redirect('/admini/managewaybill/billaccep/ma/4/id/'.$billid);
        }
    }
    
    public function scheduleapplyforAction()
    {
        $billid = $this->_getParam("id");
        $billItem = Application_Model_wayBillItem::load($billid);
        if($billItem && $billItem->status != Application_Model_wayBillItem::STATUS_Complated && $billItem->status != Application_Model_wayBillItem::Interior_Cancel){
            $billItem->addProblemWay(Application_Model_ProblemWay::Problem_ReturnCancel, Application_Model_ProblemWay::Schedule_ReturnRoute);
            $billItem->addProblemSchedule("问题运单退件申请已受理，包裹正在退回途中", Application_Model_ProblemWay::Schedule_ReturnRoute);
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function schedulereturnrouteAction()
    {
        $billid = $this->_getParam("id");
        $billItem = Application_Model_wayBillItem::load($billid);
        if($billItem && $billItem->status != Application_Model_wayBillItem::STATUS_Complated && $billItem->status != Application_Model_wayBillItem::Interior_Cancel){
            foreach ($billItem->getStoreItems() as $itemVal) {
                if ($itemVal->interior == Application_Model_StoreItem::Interior_Withhold) {
                    $itemVal->interior = Application_Model_StoreItem::Interior_ReturnIng;
                    $itemVal->save();
                }
            }
            $billItem->addProblemWay(Application_Model_ProblemWay::Problem_ReturnCancel, Application_Model_ProblemWay::Schedule_InChannel);
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function scheduleinchannelAction()
    {
        $billid = $this->_getParam("id");
        $billItem = Application_Model_wayBillItem::load($billid);
        if($billItem && $billItem->status != Application_Model_wayBillItem::STATUS_Complated && $billItem->status != Application_Model_wayBillItem::Interior_Cancel){
            foreach ($billItem->getStoreItems() as $itemVal) {
                if ($itemVal->interior == Application_Model_StoreItem::Interior_ReturnIng) {
                    $itemVal->interior = Application_Model_StoreItem::Interior_ReturnStore;
                    $itemVal->save();
                }
            }
            $billItem->addProblemWay(Application_Model_ProblemWay::Problem_ReturnCancel, Application_Model_ProblemWay::Schedule_InUuch);
            $billItem->addProblemSchedule("包裹已退到悠奇，等待入库", Application_Model_ProblemWay::Schedule_InUuch);
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function scheduleinuuchAction()
    {
        $billid = $this->_getParam("id");
        $billItem = Application_Model_wayBillItem::load($billid);
        $result = $billItem->ScheduleInUuch();
        $this->json($result);
        
    }
    
    public function partdefaultAction()
    {
        $billid = $this->_getParam("id");
        $billItem = Application_Model_wayBillItem::load($billid);
        if($billItem && $billItem->status != Application_Model_wayBillItem::Interior_Cancel){
            $billItem->addProblemWay(Application_Model_ProblemWay::Problem_PartReturn, Application_Model_ProblemWay::Schedule_ApplyFor);
            $billItem->addProblemSchedule("已申请部分不符合规范物品退回", Application_Model_ProblemWay::Schedule_ApplyFor);
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function partapplyforAction()
    {
        $billid = $this->_getParam("id");
        $billItem = Application_Model_wayBillItem::load($billid);
        if($billItem && $billItem->status != Application_Model_wayBillItem::Interior_Cancel){
            $billItem->addProblemWay(Application_Model_ProblemWay::Problem_PartReturn, Application_Model_ProblemWay::Schedule_ReturnRoute);
            $billItem->addProblemSchedule("申请部分物品退回已受理，物品正在退回途中", Application_Model_ProblemWay::Schedule_ReturnRoute);
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function partreturnrouteAction()
    {
        $billid = $this->_getParam("id");
        $billItem = Application_Model_wayBillItem::load($billid);
        if($billItem && $billItem->status != Application_Model_wayBillItem::Interior_Cancel){
            $billItem->addProblemWay(Application_Model_ProblemWay::Problem_PartReturn, Application_Model_ProblemWay::Schedule_InUuch);
            $billItem->addProblemSchedule("部分物品包裹已退到悠奇，等待入库", Application_Model_ProblemWay::Schedule_InUuch);
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function partinuuchAction()
    {
        $billid = $this->_getParam("id");
        $billItem = Application_Model_wayBillItem::load($billid);
        if($billItem  && $billItem->status != Application_Model_wayBillItem::Interior_Cancel){
            $billItem->addProblemWay(Application_Model_ProblemWay::Problem_PartReturn, Application_Model_ProblemWay::Schedule_InStore);
            $billItem->addProblemSchedule("部分物品包裹已入库,可以提交国际运单了", Application_Model_ProblemWay::Schedule_InStore);
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
    
    public function cancelreturnAction()
    {
        $billid = $this->_getParam("id");
        $billItem = Application_Model_wayBillItem::load($billid);
        if($billItem && $billItem->status != Application_Model_wayBillItem::STATUS_Complated && $billItem->status != Application_Model_wayBillItem::Interior_Cancel){
            $billItem->status = Application_Model_wayBillItem::STATUS_ExpressBack;
            $billItem->problem = Application_Model_wayBillItem::Problem_Appear;
            $billItem->save();
            $billItem->addProblemWay(Application_Model_ProblemWay::Problem_StayUser, Application_Model_ProblemWay::Schedule_Default);
            $billItem->delProblemSchedule();
            $this->json(1);
        } else {
            $this->json(0);
        }
    }
}
